let set1 = new Set();

/*  添加 */
set1.add(1)
set1.add(2)
set1.add('some text');
set1.add('some text');//去重
console.log('set1',set1);
set1.add({a:1})
set1.add({a:1})//不会去重，因为对象存的是地址，地址是不一样的
console.log('set1',set1);


 /* 删除 */
set1.delete({a:1})//不会删除
console.log('set1',set1);
set1.delete(1)
console.log('set1',set1);

 /* 查询 */
console.log(set1.has('some text'))

 /* set 是一个可迭代访问的对象，并且key 和value 一样*/
console.log('---------------------------');

for (const iterator of set1) {
    console.log(iterator);

}
for (const iterator of set1.values()) {
    console.log(iterator);

}
for (const iterator of set1.keys()) {
    console.log(iterator);

}


/* 交集 */
console.log('--------------------------------------------------');
let set2 = new Set([1,2,3,4])
console.log(set2.size)
let set3 = new Set([...set2].filter(item => set1.has(item)))
console.log('set3:',set3);

/* 差集 */

let arr4 = [...set2].filter(item => !set1.has(item));
let arr5 = [...set1].filter(item => !set2.has(item));
console.log('差集:',new Set(arr4.concat(arr5)));

/* 数组转集合 */
console.log('-------------------------------------------------------------------');
let n_arr = [2,4,'some',{b:1}];
let n_set = new Set(n_arr);
console.log(n_set);

/* 集合转数组 */
let n_arr2 = [...n_set];
console.log('n_arr2:',n_arr2);
console.log(n_arr2 === n_arr);


